﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using gnu.trove.iterator;
using gnu.trove.map.hash;
using ikvm.extensions;
using org.apache.commons.lang3;

namespace Game.utils
{
    public class AntiFlood
{
	/**
	 * Field _recentReceivers.
	 */
	private TIntLongHashMap _recentReceivers = new TIntLongHashMap();
	/**
	 * Field _lastSent.
	 */
	private long _lastSent = 0L;
	/**
	 * Field _lastText.
	 */
	private String _lastText = StringUtils.EMPTY;
	/**
	 * Field _lastHeroTime.
	 */
	private long _lastHeroTime;
	/**
	 * Field _lastTradeTime.
	 */
	private long _lastTradeTime;
	/**
	 * Field _lastShoutTime.
	 */
	private long _lastShoutTime;
	/**
	 * Field _lastMailTime.
	 */
	private long _lastMailTime;
	
	/**
	 * Method canTrade.
	 * @param text String
	 * @return boolean
	 */
	public bool canTrade(String text)
	{
		var currentMillis = java.lang.System.currentTimeMillis();
		if ((currentMillis - _lastTradeTime) < 5000L)
		{
			return false;
		}
		_lastTradeTime = currentMillis;
		return true;
	}
	
	/**
	 * Method canShout.
	 * @param text String
	 * @return boolean
	 */
	public bool canShout(String text)
	{
		var currentMillis = java.lang.System.currentTimeMillis();
		if ((currentMillis - _lastShoutTime) < 5000L)
		{
			return false;
		}
		_lastShoutTime = currentMillis;
		return true;
	}
	
	/**
	 * Method canHero.
	 * @param text String
	 * @return boolean
	 */
	public bool canHero(String text)
	{
		var currentMillis = java.lang.System.currentTimeMillis();
		if ((currentMillis - _lastHeroTime) < 10000L)
		{
			return false;
		}
		_lastHeroTime = currentMillis;
		return true;
	}
	
	/**
	 * Method canMail.
	 * @return boolean
	 */
	public bool canMail()
	{
		var currentMillis = java.lang.System.currentTimeMillis();
		if ((currentMillis - _lastMailTime) < 10000L)
		{
			return false;
		}
		_lastMailTime = currentMillis;
		return true;
	}
	
	/**
	 * Method canTell.
	 * @param charId int
	 * @param text String
	 * @return boolean
	 */
	public bool canTell(int charId, String text)
	{
		var currentMillis = java.lang.System.currentTimeMillis();
		long lastSent;
		var itr = _recentReceivers.iterator();
		var recent = 0;
		while (itr.hasNext())
		{
			itr.advance();
			lastSent = itr.value();
			if ((currentMillis - lastSent) < (text.equalsIgnoreCase(_lastText) ? 600000L : 60000L))
			{
				recent++;
			}
			else
			{
				itr.remove();
			}
		}
		lastSent = _recentReceivers.put(charId, currentMillis);
		var delay = 333L;
		if (recent > 3)
		{
			lastSent = _lastSent;
			delay = (recent - 3) * 3333L;
		}
		_lastText = text;
		_lastSent = currentMillis;
		return (currentMillis - lastSent) > delay;
	}
}

}
